'use strict';

const Service = require('egg').Service;

class TopicService extends Service {
  async getTopicCateList() {
    const list = await this.app.mysql.select('topic_tab');
    return { list };
  }

  async getTopicList(topicTabId) {
    const list = await this.app.mysql
      .query(`SELECT topic.id, publish_time, content, user_id, username, avatar_url, position,
    (SELECT COUNT(content) from topic_comment WHERE topic.id = topic_id) AS comment_count
    FROM topic INNER JOIN user ON user_id = user.id
    WHERE tab_id = ${topicTabId}`);
    return { list };
  }

  async getTopicDetail(topicId) {
    const data = await this.app.mysql
      .query(`SELECT id, publish_time, content, user_id,
    (SELECT COUNT(content) FROM topic_comment WHERE topic.id = topic_comment.topic_id) AS comment_count
    FROM topic
    WHERE id = ${topicId}`);
    return { data };
  }

  async addTopic(data) {
    const res = await this.app.mysql.insert('topic', data);
    const id = res.insertId;
    return { id };
  }

  async getTopicCommentList(topicId, userId) {
    const list = await this.app.mysql
      .query(`SELECT a.id, content, user_id, username, avatar_url, position,
    (SELECT is_like FROM topic_comment_like WHERE a.id = topic_comment_like.topic_comment_id AND user_id = ${userId}) AS is_like,
    (SELECT COUNT(user_id) FROM topic_comment_like WHERE a.id = topic_comment_like.topic_comment_id) AS like_count,
    (SELECT COUNT(content) FROM topic_comment WHERE a.id = topic_comment.reply_comment_id) AS comment_count
    FROM topic_comment a INNER JOIN user ON user_id = user.id
    WHERE topic_id = ${topicId} AND reply_comment_id = 0`);

    for (let i = 0; i < list.length; i++) {
      const replyId = list[i].id;
      const replyList = await this.app.mysql
        .query(`SELECT a.id, content, user_id, username, avatar_url, position,
        (SELECT username FROM user WHERE a.reply_user_id = user.id) AS reply_username,
        (SELECT is_like FROM topic_comment_like WHERE a.id = topic_comment_like.topic_comment_id AND user_id = ${userId}) AS is_like,
        (SELECT COUNT(user_id) FROM topic_comment_like WHERE a.id = topic_comment_like.topic_comment_id) AS like_count,
        (SELECT COUNT(content) FROM topic_comment WHERE a.id = topic_comment.reply_comment_id) AS comment_count
        FROM topic_comment a INNER JOIN user ON user_id = user.id
        WHERE topic_id = ${topicId} AND reply_comment_id = ${replyId}`);
      list[i].reply_comment = replyList;
    }
    return { list };
  }

  async addTopicComment(data) {
    const res = await this.app.mysql.insert('topic_comment', data);
    const id = res.insertId;
    return { id };
  }

  async deleteTopicComment(commentId, userId) {
    const result = await this.app.mysql.delete('topic_comment', {
      id: commentId,
      user_id: userId,
    });
    const deleteSuccess = result.affectedRows === 1;
    return { deleteSuccess };
  }

  async likeTopicComment(commentId, userId) {
    const result = await this.app.mysql.insert('topic_comment_like', {
      topic_comment_id: commentId,
      user_id: userId,
      is_like: 1,
    });
    const insertSuccess = result.affectedRows === 1;
    return { insertSuccess };
  }

  async dislikeTopicComment(commentId, userId) {
    const result = await this.app.mysql.delete('topic_comment_like', {
      topic_comment_id: commentId,
      user_id: userId,
    });
    const deleteSuccess = result.affectedRows === 1;
    return { deleteSuccess };
  }
}

module.exports = TopicService;
